package icraf.carbon.gui.model;

import icraf.carbon.core.Project;
import icraf.carbon.core.Zone.DataType;
import icraf.carbon.gui.MatrixPanel;
import icraf.carbon.gui.MenuFactory.MenuProperty;
import icraf.carbon.gui.ProjectMatrixTableModel;
import icraf.carbon.gui.ProjectPanel;
import icraf.carbon.gui.RowHighlight;
import icraf.common.gui.VerticalLabelUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeListener;

@SuppressWarnings("rawtypes")
public class MatrixIterationPanel<M extends ProjectMatrixTableModel> extends ProjectPanel implements InstantCounter<MatrixIterationPanel>{
	

//	public class ZoneTabListener implements ChangeListener {
//
//		@Override
//		public void stateChanged(ChangeEvent evt) {
//			JTabbedPane p = (JTabbedPane) evt.getSource();
//			activeZoneTabIndex  = p.getSelectedIndex();
//		}
//
//	}
	
	
	
	private static final long serialVersionUID = 3935329706944049274L;

	private boolean isRowHighlight = false;
	private Project modelProject;
	private RowHighlight rowHighlight;
//	private MouseListener rowMouseListener;
//	private TableModelListener tableModelListener;
	JTabbedPane tabPanel = new JTabbedPane(JTabbedPane.LEFT);
//	private ArrayList<ChangeListener> changeListenerList = new ArrayList<ChangeListener>();
	//TODO: default value?
//	private boolean isZoneAreaScale;
//	private int activeZoneTabIndex = 0;
//	private ZoneTabListener zoneTabListener = new ZoneTabListener();

	private InstantCounter<M> tableModelCounter;
	
	public MatrixIterationPanel(InstantCounter<M> tableModelCounter) {
		this.tableModelCounter = tableModelCounter;
		tabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		this.setLayout(new BorderLayout());
		this.add(tabPanel);
		JLabel l = new JLabel(MenuProperty.MODEL_PERIOD.getLabel()+ " ");
		l.setFont(new Font("Arial", Font.BOLD, 16));
		l.setOpaque(true);
		l.setBackground(Color.gray);
		l.setForeground(Color.white);
		l.setUI(new VerticalLabelUI());
		l.setHorizontalAlignment(JLabel.RIGHT);
		this.add(l, BorderLayout.WEST);
		
	}

//	public void fireTableDataChanged() {
//		MatrixPanel mPanel;
//		for (int i = 0; i < tabPanel.getTabCount(); i++) {
//			mPanel = (MatrixPanel) tabPanel.getComponentAt(i);
////			mPanel.fireTableDataChanged();
//		}
//	}

	public Project getModelProject() {
		return modelProject;
	}

	public int getSelectedTabIndex() {
		return tabPanel.getSelectedIndex();
	}

	public JTabbedPane getTabPanel() {
		return tabPanel;
	}

//	public void setZoneAreaScale(boolean isZoneAreaScale) {
//		this.isZoneAreaScale = isZoneAreaScale;
//		MatrixPanel mPanel;
//		for (int i = 0; i < tabPanel.getTabCount(); i++) {
//			mPanel = (MatrixPanel) tabPanel.getComponentAt(i);
//			mPanel.setZoneAreaScale(isZoneAreaScale);
//		}
//	}
	

	
	public void setCornerSetting(String unitDef, String unitArea, boolean isUnitSelectable) {
		MatrixPanel mPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			mPanel = (MatrixPanel) tabPanel.getComponentAt(i);
			mPanel.setCornerSetting( unitDef,  unitArea, isUnitSelectable);
		}
	}

//	public void setEditable(boolean b) {
//		MatrixPanel mPanel;
//		for (int i = 0; i < tabPanel.getTabCount(); i++) {
//			mPanel = (MatrixPanel) tabPanel.getComponentAt(i);
//			//mPanel.setEditable(b);
//		}
//	}

	public void setProject(Project modelProject, DataType dataType, Object attributs) {
		this.modelProject = modelProject;
		int n = -1;
		if(modelProject != null) {
			n = modelProject.getModelProject().getIterationCount();
		}
		
		while (tabPanel.getTabCount() > n + 1) {
			tabPanel.remove(tabPanel.getTabCount() - 1);
		}
		MatrixPanel mPanel;
		for (int i = 0; i < n + 1; i++) {
			if (i < tabPanel.getTabCount()) {
				mPanel = (MatrixPanel) tabPanel.getComponentAt(i);
				tabPanel.setTitleAt(i, Integer.toString(i));
			} else {
				mPanel = new MatrixPanel(tableModelCounter.createInstant());
				tabPanel.addTab(Integer.toString(i), mPanel);

//				if (rowMouseListener != null) {
//					mPanel.setRowMouseListener(rowMouseListener);
//				}
//				if (tableModelListener != null)
//					mPanel.setTableModelListener(tableModelListener);
//				

			}
			tabPanel.setEnabledAt(i, true);
			@SuppressWarnings("unchecked")
			M m = (M)mPanel.getMatrixTableModel();
			
			m.setProject(modelProject.getModelProject().getSimulatedProject(i), dataType, attributs);
			
			if (isRowHighlight) {
				mPanel.setRowHighlight(isRowHighlight, rowHighlight);
			}
			
		}

	}

	public void setRowHighlight(boolean b, RowHighlight h) {
		isRowHighlight = b;
		rowHighlight = h;
		MatrixPanel mPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			mPanel = (MatrixPanel) tabPanel.getComponentAt(i);
			mPanel.setRowHighlight(b, h);
		}

	}
	
	public void setEditable(boolean b) {
		MatrixPanel mPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			mPanel = (MatrixPanel) tabPanel.getComponentAt(i);
			@SuppressWarnings("unchecked")
			M m = (M)mPanel.getMatrixTableModel();
			m.setEditable(b);
		}
	}

	@Override
	public void setZoneAreaScale(boolean isZoneAreaScale) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addZoneTabListener(ChangeListener l) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void selectZoneTab(int i) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public MatrixIterationPanel createInstant() {
		return new MatrixIterationPanel<M>(tableModelCounter);
	}



//	public void setRowMouseListener(MouseListener rowMouseListener) {
//		this.rowMouseListener = rowMouseListener;
//	}
//
//	public void setTableModelListener(TableModelListener tableModelListener) {
//		this.tableModelListener = tableModelListener;
//	}
	
//	public void addZoneTabListener(ChangeListener l) {
//		changeListenerList.add(l);
//		MatrixPanel mPanel;
//		for (int i = 0; i < tabPanel.getTabCount(); i++) {
//			mPanel = (LCMatrixTabPanel) tabPanel.getComponentAt(i);
//			mPanel.addZoneTabListener(l);
//		}
//	}
	
//	public void selectZoneTab(int idx) {
//		MatrixPanel mPanel;
//		for (int i = 0; i < tabPanel.getTabCount(); i++) {
//			mPanel = (LCMatrixTabPanel) tabPanel.getComponentAt(i);
//			mPanel.selectZoneTab(idx);
//		}
//	}

}